#include "goodinfoeditor.h"
#include "ui_goodinfoeditor.h"
#include "qdebug.h"
#include "QString"
#include "QPixmap"
#include "QApplication"
#include "QFile"
#include "Core/ADO/goodinfodao.h"
#include "qdialog.h"
#include "QFileDialog"
#include "mainwindow.h"


GoodInfoEditor::GoodInfoEditor(EditorOpenType open_type, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::GoodInfoEditor),
    m_good_info(NULL)
{
    ui->setupUi(this);
    this->setFixedSize(this->width(),this->height());
    GoodInfoEditor::m_open_type = open_type;
    this->Init();
}

GoodInfoEditor::GoodInfoEditor(EditorOpenType open_type, GoodInfo *info, QWidget *parent):
    QDialog(parent),
    ui(new Ui::GoodInfoEditor),
    m_good_info(NULL)
{
    ui->setupUi(this);
    this->setFixedSize(this->width(),this->height());
    GoodInfoEditor::m_open_type = open_type;
    m_good_info = info;
    this->Init();
}

GoodInfoEditor::~GoodInfoEditor()
{
    delete ui;
}

void GoodInfoEditor::Init()
{
    this->setWindowTitle(m_open_type == ChangeInfo ? "修改" : "添加");
    this->ui->save_btn->setEnabled(true);
    ShowMessage(QString(""));
    if(m_open_type == ChangeInfo)
    {
        if(m_good_info == NULL)
        {
            ShowMessage(QString("must give me the right open type"));
            this->ui->save_btn->setEnabled(false);
            return;
        }
        else
        {
            this->ui->name_lab_in->setText(m_good_info->good_name);
            ui->id_lab_in->setText(m_good_info->good_id);
            ui->cost_price_lab_in->setText(QString::number(m_good_info->cost_price));
            ui->sale_price_lab_in->setText(QString::number(m_good_info->sale_price));
            ui->inventory_lab_in->setText(QString::number(m_good_info->inventory));
            ui->dis_lab_in->setText(m_good_info->description);
            SetImage(m_good_info->img_name);
        }
    }
    else if(m_open_type == AddInfo)
    {
        this->ui->name_lab_in->setText("");
        ui->id_lab_in->setText("");
        ui->cost_price_lab_in->setText("");
        ui->sale_price_lab_in->setText("");
        ui->inventory_lab_in->setText("");
        ui->dis_lab_in->setText("");
        ui->show_icon_lab_in->clear();
        ui->image_path_lab_in->setText("");
        ShowMessage("");
    }
}

void GoodInfoEditor::SetImage(QString set_img_name)
{
    // 设置图像
    QPixmap image(ui->show_icon_lab_in->width(),ui->show_icon_lab_in->height());

    // 查看图片是否存在，如果不存在则使用默认图片
    QString img_path;
    if(set_img_name.isEmpty() || set_img_name.isNull() )
    {
        img_path = DEFAULT_IMG_PATH;
    }
    else
    {
        img_path = QString("%1").arg(set_img_name);
        QFile get_img_file(img_path);
        if(!get_img_file.exists())
        {
            img_path = DEFAULT_IMG_PATH;
        }
    }

    // 加载图片
    qDebug()<<QString("img_path: %1").arg(img_path);
    image.load(img_path);
    ui->show_icon_lab_in->clear();
    ui->show_icon_lab_in->setPixmap(image);
    ui->image_path_lab_in->setText(img_path.compare(DEFAULT_IMG_PATH) == 0 ? "" :img_path);
}

void GoodInfoEditor::ShowMessage(QString msg)
{
    this->ui->message_lab->setText(msg);
}

/***************点击事件 begin*****************/

void GoodInfoEditor::on_choose_btn_clicked()
{
    QString file_name = QFileDialog::getOpenFileName(this,tr("选择要上传的图片"),"/home",tr("Images(*.jpg *.png)"));
    if(!file_name.isEmpty())
    {
        SetImage(file_name);
    }
}

void GoodInfoEditor::on_save_btn_clicked()
{
    // 图像处理
    // 如果图像途径不是当前路径则把图片拷贝到当前目录下，并且存储
    // 将图片地址转换到单独的地址
    auto info = this->GetCurGoodInfo();

    // 复制图像文件到新的文件中
    QString img_path = info.img_name;
    if(!img_path.isEmpty() && !img_path.isNull() )
    {
        QFile get_img_file(img_path);
        if(get_img_file.exists())
        {
           auto img_dir = MainWindow::GetInstance()->IMG_DIR_PATH;
           QDir dir(img_dir);
           if(!dir.exists())
           {
               dir.mkdir(img_dir);
           }

           QFileInfo fileInfo(img_path);
           auto target_path = img_dir + QString("/%1").arg(fileInfo.fileName());
           get_img_file.copy(target_path);
           info.img_name = target_path;

           qDebug() << target_path;
        }
    }

    if(m_open_type == ChangeInfo)
    {
        GoodInfoDao::GetInstance()->Update(info);
    }
    else if(m_open_type == AddInfo)
    {
        GoodInfoDao::GetInstance()->Insert(info);

        // 添加完成后自动转换为修改状态
        m_open_type = ChangeInfo;
        m_good_info = &info;
        Init();
    }
    ShowMessage(QString("完成操作"));
}

GoodInfo GoodInfoEditor::GetCurGoodInfo()
{
    GoodInfo *info = new GoodInfo();
    info->good_name = ui->name_lab_in->text();
    info->good_id = ui->id_lab_in->text();
    info->img_name = ui->image_path_lab_in->text();
    info->description = ui->dis_lab_in->toPlainText();
    info->cost_price = ui->cost_price_lab_in->text().toDouble();
    info->sale_price = ui->sale_price_lab_in->text().toDouble();
    info->inventory = ui->inventory_lab_in->text().toLong();
    return *info;
}

void GoodInfoEditor::on_close_btn_clicked()
{
    this->close();
}
/***************点击事件 end*****************/

